﻿//*******************************************************************/
//** 编 写 人：lardust-PC AFCodeAI
//** 编写日期：07/08/2013 14:01:27
//** 实现功能：
//** 修 改 人：
//** 修改日期：
//*******************************************************************/
using System.Collections;
using System.Collections.Generic;
using System.Data;
using CrazyTalk.Caching;
using CrazyTalk.Common;
using CrazyTalk.DALFactory;
using CrazyTalk.IDAL;
using CrazyTalk.Models;

namespace CrazyTalk.BLL
{	
	public class CommentBaseBLL
	{
		private static readonly CommentBaseBLL Minstance = new CommentBaseBLL();

        public static CommentBaseBLL Instance
        {
            get { return Minstance; }
        }
        private readonly ICommentBaseDAL _dal = DataAccess.Get<ICommentBaseDAL>();
        
        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(CommentBase model)
        {
            return _dal.Add(model);
        }
        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int commentBaseId)
        {
            return _dal.Delete(commentBaseId);
        }
        /// <summary>
        /// 批量数据
        /// </summary>
        public bool DeleteList(List<int> idList)
        {
            return _dal.DeleteList(idList);
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(CommentBase model)
        {
            return _dal.Update(model);
        }
        /// <summary>
        /// 是否存在指定标识记录
        /// </summary>
        public bool Exists(int commentBaseId)
        {
            return _dal.Exists(commentBaseId);
        }
        /// <summary>
        /// 获取一个对象实体
        /// </summary>
        public CommentBase GetModel(int commentBaseId)
        {
            return _dal.GetModel(commentBaseId);
        }
        /// <summary>
        /// 从缓存中获取一个对象实体
        /// </summary>
        public CommentBase GetModelByCache(int commentBaseId)
        {
            DefaultCache cache = DefaultCache.GetCacheService();
            string cacheKey = CacheKeys.GenerateCodeKey + "CommentBase/_" + commentBaseId; 
            CommentBase model = cache.RetrieveObject(cacheKey) as CommentBase;
            if (model == null)
            {
                model = _dal.GetModel(commentBaseId);
                if (model != null)
                {
                    cache.AddObject(cacheKey, model);
                }
            }
            return model;
        }
        /// <summary>
        /// 重载缓存
        /// </summary>
        public void ReLoadCache(int commentBaseId)
        {
            DefaultCache cacheService = DefaultCache.GetCacheService();
            string cacheKey = CacheKeys.GenerateCodeKey + "CommentBase/_" + commentBaseId;
            CommentBase model = _dal.GetModel(commentBaseId);
            if (model != null)
            {
                cacheService.RemoveObject(cacheKey);
                cacheService.AddObject(cacheKey, model);
            }
            else
            {
                cacheService.RemoveObject(cacheKey);
            }
        }

        public DataTable GetDetails(int commentBaseId)
        {
            return _dal.GetDetails(commentBaseId);
        }
        /// <summary>
        /// 列表分页查询
        /// </summary>
        /// <param name="queryField">查询条件的字段名称集合，字段格式：a.FieldName,>,参数名附加字符
        ///  所属表.字段名,判断限定符</param>
        /// <param name="queryValue">查询条件的值集合</param>
        /// <param name="orderby">排序列名</param>
        /// <param name="index">页码，从1开始</param>
        /// <param name="size">页记录数</param>
        /// <param name="totalCount">输出的总记录行数</param>
        /// <returns></returns>
        public DataTable GetList(List<string> queryField, List<object> queryValue, string orderby, int index, int size, out int totalCount)
        {
            return _dal.GetList(queryField, queryValue, orderby, index, size, out totalCount);
        }

        /// <summary>
        /// 列表分页查询 easyUI table 使用的json格式数据
        /// </summary>
        /// <param name="queryField">查询条件的字段名称集合，字段格式：a.FieldName,>,参数名附加字符
        ///  所属表.字段名,判断限定符</param>
        /// <param name="queryValue">查询条件的值集合</param>
        /// <param name="orderby">排序列名</param>
        /// <param name="index">页码，从1开始</param>
        /// <param name="size">页记录数</param>
        /// <returns></returns>
        public Hashtable GetList(List<string> queryField, List<object> queryValue, string orderby, int index, int size)
        {
            int totalCount;
            DataTable dtb = _dal.GetList(queryField, queryValue,orderby, index, size, out totalCount);
            Hashtable hashTable = new Hashtable
			{
				{"total", totalCount},
				{"rows", DataUtils.DataTableToArrayList(dtb,"")}
			};
            return hashTable;
        }   
	}
}